package com.web.shop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.shop.pojo.*;
import com.web.shop.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin("*")
public class ContentController {

	@Autowired
	private ContentService contentService;

	@RequestMapping("/findAllcate")
	public List<TbContentCategory> findAllcate(){
		return contentService.findAllcate();
	}

	@RequestMapping("/findAll")
	public List<TbContent> findAll(){
		return contentService.findAll();
	}
	@RequestMapping("/update")
	public RespBean update(@RequestBody TbContent content){
		if (ObjectUtils.isEmpty(content.getId())){
			contentService.add(content);
			System.out.println(content);
			return RespBean.ok("修改成功");
		}else {
			contentService.update(content);
			return RespBean.ok("修改成功");
		}
	}
	@RequestMapping("/findOne")
	public TbContent findOne(Long id){
		return contentService.findOne(id);		
	}

	@RequestMapping("/delete/{id}")
	public RespBean delete(@PathVariable Long  id){
		contentService.delete(id);
		return  RespBean.ok("删除成功");
	}
	//查询+分页
	@GetMapping("/listByPage")
	public PageResp<TbContent> listByPage(PageReq pageReq) {
		PageHelper.startPage(pageReq.getPage(),pageReq.getSize());
		Page<TbContent> page = (Page<TbContent>) contentService.findAll();
		PageResp pageResp = new PageResp();
		pageResp.setList(page.getResult());
		pageResp.setTotal(page.getTotal());
		return pageResp;
	}
	@RequestMapping("/findByCategoryId")
	public List<TbContent> findByCategoryId(Long categoryId){
		return contentService.findByCategoryId(categoryId);
	}

}
